<?php
namespace Modules\Form\Controllers;

use Core\Mvc\Controller;

class AdminController extends Controller
{
    public function indexAction($id)
    {
        extract(self::$variables['router_params']);
        $content = array();
        $formList = $this->config->get('form.userFormList');
        if (!isset($formList[$id])) {
            $this->flash->error('删除失败，表单不存在');
            return $this->notFount();
        }

        $data = $this->config->get($formList[$id]['fields']);
        if ($this->request->isPost() && $this->request->hasPost('rh')) {
            $rh = json_decode($this->request->getPost('rh'));
            $rh = jsonToHierarchy($rh);
            $newData = array();
            foreach ($rh as $key => $value) {
                $newData[$key] = $data[$key];
            }
            if ($this->config->set($formList[$id]['fields'], $newData)) {
                $this->flash->success('菜单排序成功');
                $data = $newData;
            }
        }

        self::$variables += array(
            '#templates' => 'pageNoWrapper',
            'content' => array(),
        );
        $content['formSort'] = array(
            '#templates' => 'box',
            'title' => $formList[$id]['title'] . '字段排序',
            'max' => false,
            'wrapper' => true,
            'color' => 'primary',
            'size' => '12',
            'content' => array(
                '#templates' => 'adminFormFieldSort',
                'id' => 'adminFormFieldSort',
                'title_display' => false,
                'data' => $data,
            ),
        );
        self::$variables['content'] += $content;
    }
}
